#include "Server.h"
#include "Task.h"
#include "Log.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>

#include <boost/bind.hpp>

namespace sevent{

Server::Server(const string name, const string ip, const int port)
:_name(name), _loop(ip, port), _started(false){
	_loop.setServer(this);
	_threadPool.setName(name);
}

Server::Server(const string name, const string ip, const int port, const TaskFunc &f)
:_name(name), _loop(ip, port), _taskFunc(f), _started(false){
	_loop.setServer(this);
	_threadPool.setName(name);
}

Server::~Server(){
	if(_started){
		_started = false;
		_loop.stop();
		_threadPool.stop();
	}
}

int Server::stop(){
	_started = false;
	_loop.stop();
	_threadPool.stop();
}

int Server::wait(){
	_loop.wait();
	_threadPool.wait();
}

int Server::start(int threadNum){
	LOG_INFO_VA("server starting...");
	int ret;
	
	_started = true;
	_loop.start();
	_threadPool.start(threadNum);
	return 0;
}

void Server::setTaskFunc(TaskFunc f){
	_taskFunc = f;
}

}

